import router from './router'
import store from './store'
import { getToken } from '@/utils/auth'

const whiteList = ['/login'];

router.beforeEach(async(to, from, next) => {
    const hastoken = getToken();
    if (hastoken) {
        if (to.path == "/login") {
            next({ path: '/' })
        } else {
            // 已登陆，获取用户角色
            const hasRoles = store.getters.roles && store.getters.roles.length > 0;
            if (hasRoles) {
                next()
            } else {
                // 先请求用户信息
                const { roles } = await store.dispatch('user/getInfo');
                // 根据角色生成动态路由
                const acRoutes = await store.dispatch("permission/generateRoutes", roles);
                // 添加 至router
                debugger
                acRoutes.forEach(item => {
                        router.addRoute(item)
                    })
                    // 重定向
                next({...to, replace: true })
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next(`/login?redirect=${to.path}`)
        }
    }
})